package com.meet.service;

import com.meet.dao.MeetingDao;
import com.meet.dao.MeetingDaoImpl;
import com.meet.entity.BoardRoom;
import com.meet.entity.Meeting;
import com.meet.entity.PageBean;
import com.meet.vo.MeetingConfirm;
import com.meet.vo.MeetingVo;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

/**
 * @Author LiDianyang
 * @Date 2022/8/11 21:37
 * @PackageName:com.meet.service
 * @ClassName: MeetingServiceIpml
 * @Description: TODO
 * @Version 1.0
 */
public class MeetingServiceImpl implements MeetingService{

    private MeetingDao meetingDao = new MeetingDaoImpl();

    public MeetingServiceImpl(MeetingDao meetingDao) {
        this.meetingDao = meetingDao;
    }

    @Override
    public int addMeeting(Meeting meeting) throws SQLException {
        return meetingDao.addMeeting(meeting);
    }

    @Override
    public int updateMeeting(Meeting meeting) throws SQLException {
        return meetingDao.updateMeeting(meeting);
    }

    @Override
    public int deleteMeeting(Integer id) throws SQLException {
        return meetingDao.deleteMeeting(id);
    }

    @Override
    public List<MeetingVo> getMeetingCard(int page, int number) throws SQLException {
        return meetingDao.getMeetingCard(page, number);
    }

    @Override
    public int getCount() throws SQLException {
        return meetingDao.getCount();
    }

    @Override
    public MeetingVo getMeeting(Integer Id) throws SQLException {
        return meetingDao.getMeeting(Id);
    }

    @Override
    public PageBean<MeetingVo> getAllMeetsByPage(String _currentPage, String _rows) throws SQLException {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        if(currentPage <=0) {
            currentPage = 1;
        }

        //1.创建空的PageBean对象
        PageBean<MeetingVo> pb = new PageBean<MeetingVo>();
        //2.设置参数
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //3.调用dao查询总记录数
        int totalCount = meetingDao.getCount();
        pb.setTotalCount(totalCount);

        //4.调用dao查询List集合
        //计算开始的记录索引
        int start = (currentPage - 1) * rows;
        List<MeetingVo> list = meetingDao.getMeetingCard(start,rows);
        pb.setList(list);

        //5.计算总页码
        int totalPage = (totalCount % rows)  == 0 ? totalCount/rows : (totalCount/rows) + 1;
        pb.setTotalPage(totalPage);


        return pb;
    }

    public List<Meeting> getAllMeeting(Integer id,Timestamp tStart, Timestamp tEnd) throws SQLException{
        return meetingDao.getAllMeeting(id,tStart, tEnd);
    }

    @Override
    public List<MeetingConfirm> getMeetingConfirm(boolean tag, Integer mid) throws SQLException {
        return meetingDao.getMeetingConfirm(tag, mid);
    }


}
